<?php
/*
 * Created on 19 Aug 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
require_once(dirname(dirname(__FILE__))."/common.php");
function addCourseForm(){
 	$courses = get_records_select("courses","","","coursename ASC");

	$html = '<h2>Add Course</h2>';
 	$html .= '<div class="infoholder"><div class="fieldname"><h3>Course</h3></div><p>';
 	$html .= '<select id="courseid" name="courseid">';
 	if ($courses){
	 	foreach ($courses as $course){
	 		$html .= "<option value='".$course->ident."'>".$course->coursename."</option>";
	 	}
 	}
 	$html .= '</select></p>';
 	$html .= '<div class="fieldname"><h3>Date</h3></div><p>';
	$html .= '<input type="text" name="coursedate_day" id="coursedate_day" size="1" maxlength="2" value="dd" onfocus="blankfield(\'coursedate_day\',\'dd\')"/> - ';
	$html .= '<input type="text" name="coursedate_month" id="coursedate_month" size="1" maxlength="2" value="mm" onfocus="blankfield(\'coursedate_month\',\'mm\')"/> - ';
	$html .= '<input type="text" name="coursedate_year" id="coursedate_year" size="2" maxlength="4" value="yyyy" onfocus="blankfield(\'coursedate_year\',\'yyyy\')"/>';
	$html .= '</p>';
 	$html .= '<p><input type="button" value="Add" onclick="addCourse()"/></p></div>';
 	return $html;
}

 
 
function displayUserCourses($options = true, $showifblank = true, $header = ""){
	global $CFG;
	global $page_owner;
	global $USER;
	
 	$sql = 'SELECT uc.ident, c.coursename, uc.course_date FROM '.$CFG->prefix.'courses c '.
			'INNER JOIN '.$CFG->prefix.'users_courses uc ON uc.course_id = c.ident '.
			'WHERE uc.user_id = '.$page_owner
			.' ORDER BY uc.course_date DESC';
	$courses = get_records_sql($sql);
	
	$html = "";
	if($showifblank || $courses){
		$html .= $header;
	 	$html .= '<table id="userscourses" width="100%" class="vptable"><tr id="userscoursesheader">';
	 	$html .= '<th width="20%">Date</th>';
	 	$html .= '<th>Course/Award</th>';
	 	if ($options && $page_owner == $USER->ident){
	 		$html .= '<th>Options</th>';
	 	}
	 	$html .= '</tr>';
	 	if ($courses){
		 	foreach ($courses as $course){
		 		$html .= "<tr id='courserow".$course->ident."' class='vp'>";
		 		$html .= "<td class='vp'>".formatDate($course->course_date)."</td>";
		 		$html .= "<td class='vp'>".$course->coursename."</td>";
		 		if ($options && $page_owner == $USER->ident){
		 			$html .= "<td class='vp'><span onclick='removeCourse(".$course->ident.",\"".$course->coursename."\")' class='remove'>[remove]</span></td>";
		 		}
		 		$html .= "</tr>";
		 	}
	 	}
	 	$html .= '</table>';
	}
 	return $html;
}

function addCourse($courseId, $courseDate){
	global $USER;
	global $CFG;
	$user_course = new StdClass;
	$user_course->course_id = $courseId;
	$user_course->user_id = $USER->ident;
	$user_course->course_date = $courseDate;
	insert_record('users_courses',$user_course);
	// now return the course
	$sql = 'SELECT uc.ident, c.coursename, uc.course_date FROM '.$CFG->prefix.'courses c '.
			'INNER JOIN '.$CFG->prefix.'users_courses uc ON uc.course_id = c.ident '.
			'WHERE c.ident = '.$courseId.' '.
			'AND uc.course_date = '.$courseDate.' '.
			'AND uc.user_id = '.$USER->ident;

	$course = get_record_sql($sql);
	$course->course_date = formatDate($course->course_date);
	return $course;
}


function removeCourse($usercourseId){
	global $USER;
	$where = 'ident = '.$usercourseId.' AND user_id = '.$USER->ident;
	delete_records_select('users_courses',$where);
	$delete = new StdClass;
	$delete->ident = $usercourseId;
	$delete->deleted = 'true';
	return $delete;
}

function displayAdminCourses(){
	
		global $CFG;
	$sql = 'SELECT c.ident, c.coursename, COUNT(uc.user_id) as inuse FROM '.$CFG->prefix.'courses c '.
				'LEFT OUTER JOIN '.$CFG->prefix.'users_courses uc ON c.ident = uc.course_id '.
				'GROUP BY c.ident, c.coursename ORDER BY c.coursename ASC';
				
	$courses = get_records_sql($sql);
 	$html = '<table id="userscourses" name="userscourses" width="100%"><tr>';
 	$html .= '<th>Course</th>';
 	$html .= '<th width="20%">Options</th>';
 	$html .= '</tr>';
 	if ($courses){
	 	foreach ($courses as $course){
	 		$html .= "<tr id='courserow".$course->ident."'>";
	 		$html .= "<td id='courserow".$course->ident."edit'>".$course->coursename."</td>";
	 		$html .= "<td id='courserow".$course->ident."options'>";
	 		if ($course->inuse == 0){ 	
	 			$html .= "<span onclick='editAdminCourse(".$course->ident.")' class='remove'>[edit]</span>";		
				$html .= "<span onclick='deleteAdminCourse(".$course->ident.",\"".$course->coursename."\")' class='remove'>[delete]</span>";	
	 		}
	 		$html .= "</td>";
	 		$html .= "</tr>";
	 	}
 	}
 	
 	//now add row for adding a new course
 	$html .= '<tr id="addcourserow"><td><input type="text" name="coursename" size="40" id="coursename">';
 	$html .= '<input type="button" onclick="addAdminCourse()" value="Add"></td><td></td></tr>';
 	
 	$html .= '</table>';
	return $html;
}
 
function addAdminCourse($courseName){
	global $CFG;
	$course = new StdClass;
	$course->coursename = $courseName;
	$course->last_updated = mktime();
	$newId = insert_record('courses',$course);
	
	// now return the course
	$sql = 'SELECT c.* FROM '.$CFG->prefix.'courses c '.
			'WHERE c.ident = '.$newId;
	$course = get_record_sql($sql); 
	return $course;
}

function deleteAdminCourse($courseId){
	global $CFG;	
	//first check curse isn't in use
	$sql = 'SELECT uc.course_id, COUNT(*) as inuse FROM '.$CFG->prefix.'users_courses uc '.
			'WHERE uc.course_id = '. $courseId. ' GROUP BY uc.course_id';
	$result = get_record_sql($sql);
	
	$delete = new StdClass;
	$delete->ident = $courseId;
	if ($result->inuse == 0){
		delete_records_select('courses','ident = '. $courseId);
		$delete->deleted = 'true';	
	} else {
		$delete->deleted = 'false';
		$delete->error = 'Course already in use by a user';
	}
	return $delete;
}

function editAdminCourse($courseId,$courseName){
	global $CFG;	
	//first check curse isn't in use
	$sql = 'SELECT uc.course_id, COUNT(*) as inuse FROM '.$CFG->prefix.'users_courses uc '.
			'WHERE uc.course_id = '. $courseId. ' GROUP BY uc.course_id';
	$result = get_record_sql($sql);
	
	$edit = new StdClass;
	$edit->ident = $courseId;
	if ($result->inuse == 0){
		$edit->coursename = $courseName;
		$edit->last_updated = mktime();
		update_record('courses',$edit);
		$edit->edited = 'true';	
	} else {
		$edit->edited = 'false';
		$edit->error = 'Course can\'t be edited because it\'s already in use by a user';
	}
	return $edit;
}

function getAdminCourse($courseId){
	$course = get_record_select("courses","ident=".$courseId);
	return $course;
}

?>